<template>
  <div>
    <!--搜索栏-->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.name" placeholder="请输入部门名称" clearable/>
          <el-input v-model="searchModel.manager" placeholder="请输入负责人" clearable/>
          <el-button type="primary" round icon="el-icon-search" @click="getDepartmentList">查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button type="primary" icon="el-icon-plus" circle @click="openAddUI(null)"/>
        </el-col>
      </el-row>
    </el-card>
    <!--结果，分页-->
    <el-card>
      <template>
        <el-table :data="departmentList" stripe style="width: 100%">
          <el-table-column prop="id" label="序号" width="180"/>
          <el-table-column prop="name" label="部门名称" width="180"/>
          <el-table-column prop="manager" label="部门负责人" width="180"/>
          <el-table-column prop="managerPhone" label="负责人电话" width="180"/>
          <el-table-column prop="status" label="部门状态" width="180">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
              <el-tag v-else type="danger">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openAddUI(scope.row.id)"/>
              <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteDepartment(scope.row,scope.row.status)"/>
            </template>
          </el-table-column>
          <el-table-column prop="notes" label="备注"/>
        </el-table>
      </template>
    </el-card>
    <!--分页组件-->
    <el-card>
      <el-pagination
        :current-page="searchModel.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!--用户信息编辑对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
      <el-form ref="departmentForm" :model="departmentForm">
        <el-form-item label="部门名称" :label-width="formLabelWidth">
          <el-input v-model="departmentForm.name" autocomplete="off"/>
        </el-form-item>
        <!--        <el-form-item label="密码" prop="password" :label-width="formLabelWidth">-->
        <!--          <el-input v-model="userForm.password" type="password" autocomplete="off">-->
        <!--        </el-form-item>-->
        <el-form-item label="部门负责人" :label-width="formLabelWidth">
          <el-input v-model="departmentForm.manager" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="负责人电话" :label-width="formLabelWidth">
          <el-input v-model="departmentForm.managerPhone" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="部门状态" :label-width="formLabelWidth">
          <el-switch v-model="departmentForm.status" :active-value="1" :inactive-value="0"/>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="departmentForm.notes" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDepartment">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import departmentApi from '@/api/department'

export default {
  data() {
    return {
      formLabelWidth: '130px',
      departmentForm: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        page: 1,
        pageSize: 10
      },
      departmentList: []
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    deleteDepartment(department, status) {
      if (status === 1) {
        this.$message({
          type: 'error',
          message: '该数据正在使用，无法删除'
        })
      } else {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          departmentApi.deleteDepartment(department.id).then(res => {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.getDepartmentList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    saveDepartment() {
      // 触发表单验证
      this.$refs['departmentForm'].validate((valid) => {
        if (valid) {
          console.log(0)
          // 提交请求给后台
          departmentApi.saveDepartment(this.departmentForm).then(res => {
            console.log(1)
            // 成功提示，关闭对话框，刷新数据
            this.$message({
              message: res.msg,
              type: 'success'
            })
            console.log(2)
            this.dialogFormVisible = false
            this.getDepartmentList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.departmentForm = {}
      this.$refs.departmentForm.clearValidate()
    },
    openAddUI(id) {
      this.dialogFormVisible = true
      if (id == null) {
        this.title = '新增部门'
      } else {
        this.title = '修改部门'
        departmentApi.getDepartmentById(id).then(res => {
          this.departmentForm = res.data
        })
      }
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getDepartmentList()
    },
    handleCurrentChange(page) {
      this.searchModel.page = page
      this.getDepartmentList()
    },
    getDepartmentList() {
      departmentApi.getDepartmentList(this.searchModel).then(res => {
        this.departmentList = res.data.rows
        this.total = res.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>
